<template>
  <div class="container">
    <div class="app-container">
      <el-tree v-loading="loading" :expand-on-click-node="false" default-expand-all :data="treeData" :props="defaultProps">
        <template v-slot="{node,data}">
          <el-row
            style="width: 100%;height: 40px;"
            type="flex"
            justify="space-between"
            align="middle"
          >
            <el-col>{{ data.name }}</el-col>
            <el-col :span="5">
              <span class="tree-name">{{ data.managerName }}</span>
              <el-dropdown trigger="click" @command="openDialog($event,data.id)">
                <span class="el-dropdown-link">
                  操作<i class="el-icon-arrow-down el-icon--right" />
                </span>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                  <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                  <el-dropdown-item command="del">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </el-col>
          </el-row>
        </template>
      </el-tree>
    </div>
    <AddDepart ref="addDepart" :show-dialog.sync="isShow" :pid="pid"  @updateList="getDepartList" />
  </div>
</template>
<script>
import AddDepart from './components/add-depart'
import { getDepartListApi, delDepartListApi } from '@/api/department'
import { transList2Tree } from '@/utils'
export default {
  name: 'Department',
  components: { AddDepart },
  data() {
    return {
      loading: true,
      pid: null,
      isShow: false,
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  async created() {
    this.getDepartList()
  },
  methods: {
    async getDepartList() {
      const res = await getDepartListApi()
      console.log(res)
      this.treeData = transList2Tree(res, 0)
      this.loading = false
    },
    // async delData(id) {
    //   // console.log(123)
    //   await delDepartListApi(id)
    //   this.getDepartList()
    // },
    async  openDialog(type, id) {
      if (type === 'add') {
        this.isShow = true
        this.pid = id
      } else if (type === 'edit') {
        // console.log(type)
        // id = String(id)
        this.$nextTick(() => {
          this.$refs.addDepart.getDepartInfo(id)
          this.isShow = true
        })
      } else if (type === 'del') {
        // id = String(id)
        this.$confirm('确认要删除吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          await delDepartListApi(id)
          this.$message.success('删除成功')
          this.getDepartList()
        }).catch(() => {
        })
        console.log(type)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  padding:30px 140px;
  font-size: 14px;
    .tree-name{
       width: 50px;
      display: inline-block;
      margin: 10px 40px;
  }
}
</style>
